package com.buyouchina.web.action.product;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;

import com.buyouchina.bean.area.Province;
import com.buyouchina.bean.privilege.Role;
import com.buyouchina.bean.privilege.User;
import com.buyouchina.bean.product.Outlets;
import com.buyouchina.bean.product.Outlets.OutletsType;
import com.buyouchina.service.area.ProvinceService;
import com.buyouchina.service.privilege.UserService;
import com.buyouchina.service.product.OutletsService;
import com.buyouchina.utils.Pagination;
import com.buyouchina.utils.ProvinceUtil;
import com.buyouchina.utils.SiteUrl;
import com.buyouchina.utils.UserUtil;
import com.buyouchina.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * 直销店 Action
 */
@Controller("OutletsAction")
public class OutletsAction extends BaseAction implements ModelDriven<Outlets>,
		Preparable {

	// 店铺选择
	public static final String SELECT = "outlets_select";

	private static final long serialVersionUID = 1L;

	public Log log = LogFactory.getLog(OutletsAction.class);

	private Outlets outlets = new Outlets();
	@Resource(name = "outletsServiceBean")
	private OutletsService outletsService;
	@Resource(name = "provinceServiceBean")
	private ProvinceService provinceService;
	@Resource(name = "userServiceBean")
	private UserService userService;
	private List<Outlets> outletsList = new ArrayList<Outlets>();
	private Set<Outlets> outletsSet = new HashSet<Outlets>();
	private Integer outletsid;
	private Integer userid;
	private Integer provinceid;
	private String type;

	// 进入店铺
	@SuppressWarnings("unchecked")
	public String enter() {
		outlets = outletsService.find(outletsid);
		session.put("outlets", outlets);
		return SUCCESS;
	}

	// 查询区域全部店铺
	public String findAll() {
		Integer epid;
		User user = (User) session.get("user");
		Pagination pagination = new Pagination(request, response);

		List<Province> provinceList = new ArrayList<Province>(user
				.getProvinces());
		if (provinceList.size() > 0) {
			Integer pid = provinceList.get(0).getId();
			if(UserUtil.getMaxLevel(user)==7000){
				 epid = ProvinceUtil.getLastIndexOfProvince(provinceService, pid);
			}else if(UserUtil.getMaxLevel(user)==6000){
				epid = ProvinceUtil.getLastIndexOfCity(provinceService, pid);
			}
			else{
				epid=pid;
			}
			pagination.setRecordCount(outletsService.getRecordCount(pid, epid));
			outletsList = outletsService.list(pagination.getFirstResult(),
					pagination.getPageSize(), pid, epid);
		} else {
			Long count = outletsService.getCount();
			pagination.setRecordCount(count.intValue());
			outletsList = outletsService.getScrollData(
					pagination.getFirstResult(), pagination.getPageSize())
					.getResultlist();
		}

		request.setAttribute("pagination", pagination);
		return "list";
	}

	// 查询当前用户关联的店铺列表
	@SuppressWarnings("unchecked")
	public String select() {
		User user = (User) session.get("user");
		outletsList = outletsService.list(user.getId());
		if (outletsList.size() > 1) {
			return SELECT;
		} else if (user.getOutlets().size() > 0) {
			for (Outlets outlets : user.getOutlets()) {
				session.put("outlets", outlets);
			}
			return SUCCESS;
		}
		request.setAttribute("message", "<strong>出错啦	!</strong>您还没有店铺,请联系总部!");
		request.setAttribute("urladdress", SiteUrl.readUrl("logon"));
		return MESSAGE;
	}

	public String query() {
		String hql = "from Outlets o where o.name like ?1";
		outletsList = outletsService.list(hql, "%" + outlets.getName() + "%");
		return "list";
	}

	// 按区域查询店铺
	public String listByArea() {
		User user=(User) session.get("user");
		outletsList = outletsService.listByArea(provinceid, ProvinceUtil
					.getLastIndexOfProvince(provinceService, provinceid),user);
		return "storeSelect";
	}

	// 我的店铺
	public String myOultets() {
		return "myoutlets";
	}

	// 加载店铺信息
	public String getInfo() {
		return "info";
	}

	// 修改店长ui
	public String modifyManager() {
		outlets = outletsService.find(outletsid);
		return "modifyManager";
	}

	// 设置店长
	public String managerSet() {
		User manager = userService.find(userid);
		outlets = outletsService.find(outletsid);
		outlets.getUsers().remove(outlets.getManager());// 把之前的店长中店铺中删掉
		if(outlets.getType().equals(OutletsType.STORELESS)){
			outlets.setProxyManager(manager);
		}else{
			outlets.setManager(manager);// 设置新的店长
		}
		outlets.getUsers().add(manager);// 将新店长加到店里人员中
		outletsService.update(outlets);
		request.setAttribute("message", "店长设定成功");
		request.setAttribute("urladdress", SiteUrl.readUrl("outlets.list"));
		return MESSAGE;
	}

	// 修改店铺信息
	public String modify() {
		String fax = outlets.getFax();
		String address = outlets.getAddress();
		String telphone = outlets.getTelphone();
		outlets = (Outlets) session.get("outlets");
		outlets.setFax(fax);
		outlets.setAddress(address);
		outlets.setTelphone(telphone);
		outletsService.update(outlets);
		request.setAttribute("message", "店铺信息修改成功");
		request.setAttribute("urladdress", SiteUrl.readUrl("outlets.info"));
		return MESSAGE;
	}

	// 创建店铺视图
	public String add() {
		return "add";
	}

	// 创建店铺
	public String save() {
		User user = (User) session.get("user");

		Province province = provinceService.find(outlets.getProvince().getId());
		if(type.equals("STORE")){
			outlets.setType(OutletsType.STORE);
		}else{
			outlets.setType(OutletsType.STORELESS);
		}
		outlets.setProvince(province);
		outlets.setCreater(userService.find(user.getId()));
		outlets.setDate(new Date());
		outletsService.save(outlets);

		request.setAttribute("message", "店铺创建成功");
		request.setAttribute("urladdress", SiteUrl.readUrl("outlets.list"));
		return MESSAGE;
	}

	// 删除店铺
	public String del() {
		outletsService.delete(outlets.getId());
		request.setAttribute("message", "店铺删除成功");
		request.setAttribute("urladdress", SiteUrl.readUrl("outlets.list"));
		return MESSAGE;
	}

	// override
	public Outlets getModel() {
		return outlets;
	}

	public void prepare() throws Exception {
		if (outletsid == null || "".equals(outletsid)) {
			outlets = new Outlets();
		} else {
			outlets = outletsService.find(outletsid);
		}
	}

	// getter and setter
	public List<Outlets> getOutletsList() {
		return outletsList;
	}

	public void setOutletsList(List<Outlets> outletsList) {
		this.outletsList = outletsList;
	}

	public Integer getOutletsid() {
		return outletsid;
	}

	public void setOutletsid(Integer outletsid) {
		this.outletsid = outletsid;
	}

	public Outlets getOutlets() {
		return outlets;
	}

	public void setOutlets(Outlets outlets) {
		this.outlets = outlets;
	}

	public Set<Outlets> getOutletsSet() {
		return outletsSet;
	}

	public void setOutletsSet(Set<Outlets> outletsSet) {
		this.outletsSet = outletsSet;
	}

	public Integer getUserid() {
		return userid;
	}

	public void setUserid(Integer userid) {
		this.userid = userid;
	}

	public Integer getProvinceid() {
		return provinceid;
	}

	public void setProvinceid(Integer provinceid) {
		this.provinceid = provinceid;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
